import Vue from 'vue'
import VueRouter from 'vue-router'
import constantRoutes from './constant'
import asyncRoutes from './asyncs'
import { routesWhiteList } from '@/config'
Vue.use(VueRouter)

const routes = [...constantRoutes, ...asyncRoutes]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
})
router.beforeEach((to, from, next) => {
  // 路由守卫
  const isLogin = sessionStorage.userInfo ? true : false
  if (routesWhiteList.find((item) => item === to.path)) {
    next()
  } else {
    isLogin ? next() : next('/login')
  }
})
export default router
